package com.yejhxt.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.yejhxt.inter.deviceMapper;
import com.yejhxt.inter.messageMapper;
import com.yejhxt.inter.roleMapper;
import com.yejhxt.inter.sosMapper;
import com.yejhxt.inter.userMapper;
import com.yejhxt.model.device;
import com.yejhxt.model.role;
import com.yejhxt.model.sos;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;
@Controller
@RequestMapping(value = "api/sysuser", method = RequestMethod.POST)
public class sysManager {
	@Resource
	private userMapper userMapper;
	@Resource
	private roleMapper roleMapper;
	@Resource
	private deviceMapper deviceMapper;
	@Resource
	private messageMapper messageMapper;
	@Resource
	private sosMapper sosMapper;


	@RequestMapping(value = "getAlluser.do")
	public void getAlluser( HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		
	 int status;
	 List<role> roles = null;
		if (request.getRemoteUser() == null)
			status = 403;

		else {
			roles= roleMapper.selectAll();
		   
			status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200&&roles!=null) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", roles);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "no user");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/getmessage.do")
	public void getoneinfo(
			@RequestParam(value = "devicecode", defaultValue = "") String devicecode,
			@RequestParam(value = "dateBegin", defaultValue = "") String dateBegin,
			@RequestParam(value = "dateEnd", defaultValue = "") String dateEnd,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) throws ParseException {
		 device m = null;
		 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm");
		int status = 403;
	
		if (request.getRemoteUser() == null)
			status = 403;
		else if (StringUtils.isEmpty(devicecode)&&StringUtils.isEmpty(dateBegin)&&StringUtils.isEmpty(dateEnd))
			status = 406;
		else {
			Map<String, Object> map1=new HashMap<>();
			map1.put("devicecode",devicecode);
			List<device> device =deviceMapper.selectByConditions(map1);
			if(device!=null)
			{
				map1.clear();
			map1.put("begin", s.parse(dateBegin));
			map1.put("end", s.parse(dateEnd));
			map1.put("deviceid", device.get(0).getDeviceid());
		    m=deviceMapper.selectByPrimaryKeyWithMessages(map1);
		 status = 200;
			}
		}
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200&&m!=null) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", m);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "暂无信息");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/getsosmessage.do")
	public void getsosmessage(
			
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) throws ParseException {
		 List<sos> m = null;
		 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm");
		int status = 403;
	
		if (request.getRemoteUser() == null)
			status = 403;
		else {
	
		
		 m=sosMapper.selectAll();
		 status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200&&m!=null) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", m);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "changesos.do")
	public void changesos(
			@RequestParam(value = "sosiid", defaultValue = "") String sosiid,
			HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response) {
		int status = 403;
		role role = null;
		
		if (request.getRemoteUser() == null)
			status = 403;
		else {

			sos s=new sos();
			s.setSosiid(new Long(sosiid));
			s.setStatus(3);
			sosMapper.updateByPrimaryKeySelective(s);

			status = 200;
		}

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", role);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {

			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
}
